<?php
namespace app\extend\express;

class Express{

    protected static $appId;      //商户id
    protected static $appKey;     //API Key
    protected static $reqURL;     //回调地址


    function __construct() {
        self::$appId = config('express.appid');
        self::$appKey = config('express.appkey');
    }

    /**
     * @param 快递单号 75111252607589
     * @return list
     *  查询订单物流轨迹
     */
    public static function getTraces($LogisticCode=0){
        $ShipperCode = self::getCode($LogisticCode);

        if(!$ShipperCode){
            return false;   //单号未查询到物流公司
        }
        $requestData= "{'OrderCode':'','ShipperCode':'". $ShipperCode ."','LogisticCode':'" .$LogisticCode. "'}";
        //测试
        $_url = "http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json";
        $url = "http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
        $data = array(
            'EBusinessID' => self::$appId,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData),
            'DataType' => '2',
        );
        $data['DataSign'] = self::encrypt($requestData, self::$appKey);
        $response = self::curl_request($url, $data);

        if (is_array($response) && isset($response['Success'])) {
            return $response['Traces'];
        } else {
            return false;
        }

    }

    /**
     * 单号识别
     *
     * @param $logisticCode string 快递单号
     *
     * @return string|null
     */
    private static function getCode($LogisticCode)
    {
        //测试
        $_url = "http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json";
        //正式
        $url = "http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
        $requestData= "{'LogisticCode':'".$LogisticCode."'}";
        $data = array(
            'EBusinessID' => self::$appId,
            'RequestType' => '2002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $data['DataSign'] = self::encrypt($requestData, self::$appKey);
        $response = self::curl_request($url, $data);

        if (is_array($response) && isset($response['Success']) && isset($response['Shippers'][0]['ShipperCode'])) {

            return $response['Shippers'][0]['ShipperCode'];

        } else {
            return false;
        }
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param appkey Appkey
     * @return DataSign签名
     */
    private static function encrypt($data, $appkey) {
        return urlencode(base64_encode(md5($data.$appkey)));
    }


    //参数1：访问的URL，参数2：post数据(不填则为GET)
    private static function curl_request($url,$post=[]){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if($post) {

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        }

        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        return json_decode($data,true);
    }



}